package com.hjq.www.View;

import com.hjq.www.Dao.Impl.UserRegisterDao;
import com.hjq.www.po.User;
import com.hjq.www.utils.JDBCUtils;
import com.hjq.www.utils.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Create with IntelliJ IDEA
 *
 * @author: hjq
 * @Date:
 * @Description:
 */
public class RegisterView extends JFrame {
    private final Container container = getContentPane();
    private final JLabel usernameLabel = new JLabel("用 户 名: ");
    private final JTextField usernameField = new JTextField(15);
    private final JLabel pwdLabel = new JLabel("密    码: ");
    private final JTextField pwdField = new JTextField(15);
    private final JLabel phoneLabel = new JLabel("联系电话: ");
    private final JTextField phoneField = new JTextField(15);
    private final JLabel idnumLabel = new JLabel("身份证号: ");
    private final JTextField idnumField = new JTextField(15);
    private final JButton okButton = new JButton("确定");
    private final JButton cancelButton = new JButton("取消");
    private final JButton searchButton = new JButton("查看");
    private final JButton updateButton = new JButton("修改");
    private final JButton resetButton = new JButton("重置");
    Connection conn = null;
    PreparedStatement ps=null;
    ResultSet rs = null;
    UserRegisterDao u = new UserRegisterDao();
    public void RegisterView1(String title) {
        //设置窗口大小
        setBounds(570, 130, 450, 500);
        //添加一块桌布
        container.setLayout(new BorderLayout());
        //初始化窗口
        init();
        //设计窗口可见
        setVisible(true);
    }
    public void init() {
        JPanel root = new JPanel();
        root.setLayout(null);
        Font f = new Font("宋体", Font.PLAIN, 25);
        usernameLabel.setBounds(10, 40, 130, 60);
        usernameField.setBounds(130, 53, 280, 40);
        pwdLabel.setBounds(10, 120, 130, 60);
        pwdField.setBounds(130, 133, 280, 40);
        phoneLabel.setBounds(10, 200, 130, 60);
        phoneField.setBounds(130, 213, 280, 40);
        idnumLabel.setBounds(10, 280, 130, 60);
        idnumField.setBounds(130, 293, 280, 40);
        usernameLabel.setFont(f);
        pwdLabel.setFont(f);
        phoneLabel.setFont(f);
        idnumLabel.setFont(f);
        root.add(usernameLabel);
        root.add(usernameField);
        root.add(pwdLabel);
        root.add(pwdField);
        root.add(phoneLabel);
        root.add(phoneField);
        root.add(idnumLabel);
        root.add(idnumField);
        okButton.setBounds(80, 370, 100, 50);
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                registerActionPerformed(e);
            }
        });
        cancelButton.setBounds(250, 370, 100, 50);
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginView();
                dispose();
            }
        });
        root.add(okButton);
        root.add(cancelButton);
        container.add(root);
    }

    private void registerActionPerformed(ActionEvent e) {
        String username = this.usernameField.getText();
        String password = this.pwdField.getText();
        String phone = this.phoneField.getText();
        String idnum =this.idnumField.getText();
        if (StringUtil.isEmpty(username)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空!");
            return;
        }
        if (StringUtil.isEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空!");
            return;
        }
        if (StringUtil.isEmpty(password)) {
            JOptionPane.showMessageDialog(null, "联系电话不能为空!");
            return;
        }
        if (StringUtil.isEmpty(password)) {
            JOptionPane.showMessageDialog(null, "身份证号不能为空!");
            return;
        }
        try {
            User user = new User(username, password,phone,idnum);
            conn = JDBCUtils.getConnection();
            int result= u.register(conn,user);
            if (result != 0) {
                JOptionPane.showMessageDialog(null, "注册成功");
                new LoginView();
                dispose();
            }else{
                JOptionPane.showMessageDialog(null, "注册失败");
            }
        } catch (HeadlessException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 根据输入的用户名查看和修改个人信息
     */
    public void UserView(){
        Container container=getContentPane();
        setBounds(570, 130, 450, 500);
        container.setLayout(new BorderLayout());
        setTitle("个人信息(输入用户名进行操作)");
        JPanel j = new JPanel();
        j.setLayout(null);
        Font f = new Font("宋体", Font.PLAIN, 25);
        usernameLabel.setBounds(10, 40, 130, 60);
        usernameField.setBounds(130, 53, 280, 40);
        pwdLabel.setBounds(10, 120, 130, 60);
        pwdField.setBounds(130, 133, 280, 40);
        phoneLabel.setBounds(10, 200, 130, 60);
        phoneField.setBounds(130, 213, 280, 40);
        idnumLabel.setBounds(10, 280, 130, 60);
        idnumField.setBounds(130, 293, 280, 40);
        usernameLabel.setFont(f);
        pwdLabel.setFont(f);
        phoneLabel.setFont(f);
        idnumLabel.setFont(f);
        j.add(usernameLabel);
        j.add(usernameField);
        j.add(pwdLabel);
        j.add(pwdField);
        j.add(phoneLabel);
        j.add(phoneField);
        j.add(idnumLabel);
        j.add(idnumField);
        searchButton.setBounds(50, 370, 100, 50);
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                search();
            }
        });
        updateButton.setBounds(170, 370, 100, 50);
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });
        resetButton.setBounds(290,370,100,50);
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        j.add(searchButton);
        j.add(updateButton);
        j.add(resetButton);
        container.add(j);
        setVisible(true);
    }
    public void search(){
        String name=usernameField.getText();
        boolean flag=false;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "select * from user where username=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            rs = ps.executeQuery();
            while(rs.next()){
                String sName = rs.getString("username");
                if(name.equals(sName)){
                    usernameField.setText(rs.getString("username"));
                    pwdField.setText(rs.getString("pwd"));
                    phoneField.setText(rs.getString("phone"));
                    idnumField.setText(rs.getString("idnum"));
                    flag=true;
                }
            }
            if(flag) {
                JOptionPane.showMessageDialog(null, "查询成功");
            }else{JOptionPane.showMessageDialog(null,"查询失败");}
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"查询失败");
            e.printStackTrace();
        }finally{
            JDBCUtils.close(conn,ps,rs);
        }
    }

    public void update(){
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql="update user set username=?,pwd=?,phone=?,idnum=? where username=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,usernameField.getText());
            ps.setString(2,pwdField.getText());
            ps.setString(3,phoneField.getText());
            ps.setString(4,idnumField.getText());
            ps.setString(5,usernameField.getText());
            ps.executeUpdate();
            JOptionPane.showMessageDialog(null, "修改成功");
        }catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "修改失败");
            e.printStackTrace();
        }finally{
            JDBCUtils.close(conn,ps);
        }
    }
    public void reset(){
        usernameField.setText("");
        pwdField.setText("");
        phoneField.setText("");
        idnumField.setText("");
    }
    public void add(JTextField usernameField,JTextField pwdField,JTextField phoneField,JTextField idnumField){
        try{
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into user(username,pwd,phone,idnum) value(?,?,?,?) where username=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,usernameField.getText());
            ps.setString(2,pwdField.getText());
            ps.setString(3,phoneField.getText());
            ps.setString(4,idnumField.getText());
            ps.setString(5,usernameField.getText());
            ps.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            try{
                if (conn != null){
                    conn.close();
                }
                }catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    public void delete(JTextField usernameField){
        try{
            conn = JDBCUtils.getConnection();
            String sql="delete from user where username=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,usernameField.getText());
            ps.execute();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "修改失败");
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        RegisterView r=new RegisterView();
        r.UserView();
    }
}
